package com.burnerpat.fl2d.core.entity;

import com.burnerpat.fl2d.core.IUpdatable;
import com.burnerpat.fl2d.core.shape.Shape;
import com.burnerpat.fl2d.math.Vector2D;

public class DynamicEntity extends PhysicalEntity implements IUpdatable
{
	protected Vector2D speed = null;
	
	public static final long INFINITE = -9999;
	private long lifetime = INFINITE;
	
	private boolean alive = false;
	
	public DynamicEntity()
	{
		super();
	}
	
	public DynamicEntity(long pId, Vector2D position, double alpha, Vector2D pSpeed, long pLifetime, Shape pShape)
	{
		super(pId, position, alpha, pShape);
		
		speed = pSpeed;
		lifetime = pLifetime;
		
		if (lifetime > 0 || lifetime == INFINITE)
		{
			alive = true;
		}
	}
	
	public Vector2D getSpeed()
	{
		return speed;
	}
	
	public void setSpeed(Vector2D pSpeed)
	{
		speed = pSpeed;
	}
	
	public long getLifetime()
	{
		return lifetime;
	}
	
	public void setLifetime(long pLifetime)
	{
		lifetime = pLifetime;
	}
	
	public boolean isAlive()
	{
		return alive;
	}
	
	public boolean isDead()
	{
		return !alive;
	}
	
	public void die()
	{
		lifetime = 0;
		alive = false;
	}
	
	public void spawn(Vector2D pPosition, double pAlpha, Vector2D pSpeed, long pLifetime)
	{
		position = pPosition;
		alpha = pAlpha;
		speed = pSpeed;
		lifetime = pLifetime;
		
		alive = true;
	}
	
	public void spawn(Vector2D pPosition, double pAlpha, Vector2D pSpeed)
	{
		spawn(pPosition, pAlpha, pSpeed, INFINITE);
	}
	
	public void spawn(Vector2D pPosition, double pAlpha)
	{
		spawn(pPosition, pAlpha, null, INFINITE);
	}
	
	@Override
	public void update(double stepSize, long tick)
	{
		if (speed != null)
		{
			position = position.add(speed);
		}
		
		if (lifetime != INFINITE)
		{
			lifetime -= tick;
			
			if (lifetime < 0)
			{
				lifetime = 0;
				
				die();
			}
		}
	}
}
